<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增债权交易')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-transaction-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">主债权人名称：</label>
                <div class="col-sm-8">
                    <input name="creditorName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">主债权人联系电话：</label>
                <div class="col-sm-8">
                    <input name="creditorPhone" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">民事判决书（或者仲裁书、调解书）字号：</label>
                <div class="col-sm-8">
                    <input name="civilJudgmentFont" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">执行法院：</label>
                <div class="col-sm-8">
                    <input name="compententCourt" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">判决书标的额：</label>
                <div class="col-sm-8">
                    <input name="debtAmount" class="form-control" type="text">
                </div>
            </div>
            <!--            <div class="form-group">    -->
            <!--                <label class="col-sm-3 control-label">保证金：</label>-->
            <!--                <div class="col-sm-8">-->
            <!--                    <input name="bond" class="form-control" type="text">-->
            <!--                </div>-->
            <!--            </div>-->
            <div class="form-group">
                <label class="col-sm-3 control-label">转让金额：</label>
                <div class="col-sm-8">
                    <input name="transferAmount" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">债务人信息：</label>
                <div class="col-sm-8">
                    <table style="font-weight: normal;" id="debtorPerson">
                        <thead>
                        <tr>
                            <th>姓名/组织机构名称</th>
                            <th>身份证号/社会统一信用代码</th>
                            <th>身份证/营业执照有效期</th>
                            <th>债务人所在地址</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <input id="user_size" type="hidden" value="1">
                        <tbody>
                        <tr id="tr_1">
                        <td><input type="text" name="name"/></td>
                        <td><input type="text" name="idNum"/></td>
                        <td><input type="text" name="idValidDate"/></td>
                        <td><input type="text" name="address"/></td>
                        <td><a class="layui-layer-btn0" onclick='addUser(this,1)'>添加一行</a></td>
                        </tr>
                        </tbody>
                    </table>

                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">债权人的身份证明材料：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="idCardFront">
                    <div class="file-loading">
                        <input class="file-upload" id="idCardBack" name="file" type="file">
                    </div>
                </div>
            </div>
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label">债务人身份证正面：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input type="hidden" name="idCardBack">-->
<!--                    <div class="file-loading">-->
<!--                        <input class="file-upload" id="idCardBack" name="file" type="file">-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label">债权人和债务人营业执照：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input type="hidden" name="licensePhoto">-->
<!--                    <div class="file-loading">-->
<!--                        <input class="file-upload" id="licensePhoto" name="file" type="file">-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
            <div class="form-group">
                <label class="col-sm-3 control-label">民事判决书：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="civilJudgement">
                    <div class="file-loading">
                        <input class="file-upload" id="civilJudgement" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">委托书：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="attorneyPower">
                    <div class="file-loading">
                        <input class="file-upload" id="attorneyPower" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">承诺书：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="statement">
                    <div class="file-loading">
                        <input class="file-upload" id="statement" name="file" type="file">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "business/transaction"
        $("#form-transaction-add").validate({
            focusCleanup: true
        });
        var recieverArr = []; //全局变量
        function recieverMsgToJson(){  //若有多个表单公用这个函数，这里需要传所属表单的ID；例如新增和修改。
            $("#debtorPerson tr:gt(0)").each(function(m){  //遍历每个li，当前有两个li
                var recieverAttributes = [];
                $(this).find("td").each(function(n){  //遍历每个li下的span，而每个li下有三个span
                    recieverAttributes[n] = $(this).children("input").val();  //找到每个span下存放着数据的input框，并获取值存放到数组中
                });
                var recieverObj = {  //用对象来表示数据；这时对象是{recieverName:小明,recieverPhone:12345678,recieverAddress:中国上海}
                    name:recieverAttributes[0],
                    idNum:recieverAttributes[1],
                    idValidDate:recieverAttributes[2],
                    address:recieverAttributes[3]
                };
                recieverArr.push(recieverObj);
            });
        }
        function submitHandler() {
            recieverMsgToJson();
            var submitData = $('#form-transaction-add').serializeJSON();
            submitData.debtorList = recieverArr;
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", JSON.stringify(submitData),'',{contentType : 'application/json;charset=utf-8'});
            }
        }

        $("input[name='creditorRightsTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='approvalTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='payTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        var fileList = new Array();
        var fileName = new Array();
        $(".file-upload").fileinput({
            uploadUrl: ctx + 'common/upload',
            maxFileCount: 1,
            autoReplace: true,
        }).on('fileuploaded', function (event, data, previewId, index) {
            fileList.push(data.response.url)
            fileName.push(data.response.name)
            $("input[name='" + event.currentTarget.id + "']").val(fileList)
            $("#"+$(this).attr("id")).val(data.response.url)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        }).on('filesuccessremove', function(event, id) {
            deleteArray(event, id);
        });
        function deleteArray(event, id){
            var name = id.split("_")[1];
            var index = fileName.indexOf(name);
            if(fileList.length > 0 && fileName.length > 0){
                fileList.splice(index,index);
                fileName.splice(index,index);
                $("input[name='" + event.currentTarget.id + "']").val(fileList)
                $("#fileName").val(fileName)
            }
        }
        function addUser(ths,line) {
            var button_value = $(ths).html();
            //获取当前点击的tr
            var tr = ths.parentNode.parentNode;
            //获取当前点击的tr上的属性id的值
            var tr_id = $(tr).attr("id");//tr_1
            //截取的方式：addUser(ths),这种方式不用传递过来line行号。
            //var _n=tr_id.lastIndexOf("_");//截取，
            //var line=tr_id.substring(_n+1);//获取到数字

            if (button_value == "添加一行") {
                //获取当前行号,行号替换规则
                var regS = new RegExp(line, "gi");//regS = /1/gi; 正则表达式，g:全局，i：忽略大小写。
                //获取要替换的内容,或记录当前的行数，为避免重复，只增加，不减少
                var current_tr_line = parseInt($("#user_size").val()) + 1;//主要用来存储当前保存的页码数字
                //更新行数
                $("#user_size").val(current_tr_line);
                //获取当前点击的tr所包含的html---html.prop("outerHTML")
                var tr_html = $("#" + tr_id).prop("outerHTML");
                //对获取的内容进行替换   .replace("添加一行", "删除本行")，替换文章和行号
                var input_html = tr_html.replace(button_value, "删除本行").replace(regS, current_tr_line);
                //写入记录
                $("table tbody").append(input_html);
                //清空INPUT数据
                //var ids = $("table tr:last").attr("id");
                //$("#"+ids+" td input").val("");
            } else {//点击删除按钮
                $("tr[id=" + tr_id + "]").remove();
            }
        }

        $(function() {
            $('.summernote').summernote({
                lang: 'zh-CN',
                height:194,
                dialogsInBody: true,
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                        var obj = this;
                        var data = new FormData();
                        data.append("file", files[0]);
                        $.ajax({
                            type: "post",
                            url: ctx + "common/upload",
                            data: data,
                            cache: false,
                            contentType: false,
                            processData: false,
                            dataType: 'json',
                            success: function(result) {
                                if (result.code == web_status.SUCCESS) {
                                    $('#' + obj.id).summernote('insertImage', result.url);
                                } else {
                                    $.modal.alertError(result.msg);
                                }
                            },
                            error: function(error) {
                                $.modal.alertWarning("图片上传失败。");
                            }
                        });
                    }
                }
            });
        });
    </script>
</body>
</html>